<?php

//
// [signals.php]
// Unix signal handling.
//
// Ruffy IRC Bot
//

function signal_handler($signo) {
	global $socket;

	log_debug("I got signal number: $signo.");
	switch ($signo) {
		case SIGHUP: // 1
			log_info("Shutting down the bot...");
			send("QUIT :Caught SIGUP");
			fclose($socket);
			log_info("Done.");
			exit;
			break;
		case SIGINT: // 2
			log_info("Shutting down the bot...");
			send("QUIT :Caught SIGINT");
			fclose($socket);
			log_info("Done.");
			exit;
			break;
		case SIGTERM: // 15
			log_info("Shutting down the bot...");
			send("QUIT :Caught SIGTERM");
			fclose($socket);
			log_info("Done.");
			exit;
			break;
		case SIGUSR1: // 30, 10, 16
			flush();
			log_info("Flushing all output buffers.");
			break;
	}
}

log_debug("Installing signal handlers.");

pcntl_signal(SIGHUP, "signal_handler");
pcntl_signal(SIGINT, "signal_handler");
pcntl_signal(SIGTERM, "signal_handler");
pcntl_signal(SIGUSR1, "signal_handler");

?>
